package com.wl.dzt.sys.core.dao.curd.sql.el;

import com.wl.dzt.sys.core.dao.select.term.Term;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description sql查询构建
 * 根据条件占位符进行一次sql语句的构建
 */
public class SqlParamBuilder {
    /**
     * sql参数
     */
    private SqlParam sqlParam;

    @SuppressWarnings("deprecation")
    public SqlParamBuilder(String sql) {
        this.sqlParam = new SqlParam(sql);
    }

    /**
     * <b> 添加结束sql</b>
     * <p>
     * 添加结束的sql语句
     * </p>
     *
     * @param sql
     * @return
     */
    public SqlParamBuilder addEndSql(String sql) {
        this.sqlParam.setSql(this.sqlParam.getSql() + "  " + sql);
        return this;
    }

    /**
     * <b> 添加开始sql </b>
     * <p>
     * 添加开始的sql语句
     * </p>
     *
     * @param sql
     * @return
     */
    public SqlParamBuilder addStartSql(String sql) {
        this.sqlParam.setSql(sql + "  " + this.sqlParam.getSql());
        return this;
    }

    /**
     * <b> 根据占位符添加sql</b>
     * <p>
     * 根据占位符添加sql语句的查询条件
     * </p>
     *
     * @param index
     * @param sql
     * @return
     */
    public SqlParamBuilder addPositionSql(int index, String sql) {
        this.sqlParam.setSql(this.sqlParam.getSql().replaceAll("{" + index + "}", sql));
        return this;
    }

    /**
     * <b>创建sql查询条件</b>
     * <p>
     * 根据条件和分页信息进行一次sql查询条件的创建
     * </p>
     *
     * @param list
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    public SqlParam create(List<Term> list, int start, int limit) throws Exception {
        this.sqlParam.setStart(start);
        this.sqlParam.setLimit(limit);
        this.sqlParam.setTerms(list);
        if (this.sqlParam.getTerms() != null && !this.sqlParam.getTerms().isEmpty()) {
            String sql = this.sqlParam.getSql();
            Map<String, List<Term>> groupTerm = SqlGroup.group(this.sqlParam.getTerms());
            for (String key : groupTerm.keySet()) {
                String builderSql = SqlFormat.builderWheres(groupTerm.get(key));
                if (!"@".equals(key)) {
                    sql = sql.replace("{" + key + "}", builderSql);
                } else {
                    sql = sql.replace("@", builderSql);
                }
            }
            this.sqlParam.setFullSql(sql.replace("@", ""));
        } else {
            this.sqlParam.setFullSql(this.sqlParam.getSql().replace("@", ""));
        }
        Pattern p = Pattern.compile("(\\{[^{}]*\\})+");
        Matcher m = p.matcher(this.sqlParam.getFullSql());
        while (m.find()) {
            String rex = m.group(1);
            this.sqlParam.setFullSql(this.sqlParam.getFullSql().replace(rex, ""));
        }
        return this.sqlParam;
    }

    /**
     * <b>建sql查询条件</b>
     * <p>
     * 无查询条件进行一次分页条件创建
     * </p>
     *
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    public SqlParam create(int start, int limit) throws Exception {
        return create(null, start, limit);
    }

    /**
     * <b> 创建sql参数对象</b>
     * <p>
     * 无分页进行一次条件查询
     * </p>
     *
     * @param terms
     * @return
     * @throws Exception
     */
    public SqlParam create(List<Term> terms) throws Exception {
        return create(terms, 0, -1);
    }

    /**
     * <b>创建sql参数对象</b>
     * <p>
     * 无分页 无查询条件
     * </p>
     *
     * @return
     * @throws Exception
     */
    public SqlParam create() throws Exception {
        return create(null, 0, -1);
    }
}
